﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.BLL;
using AiXiu.Model;
using AiXiu.DAL;
using AiXiu.Common;

namespace AiXiu.BLL
{
    public class LoginBLL
    {
        AiXiuDbContext db = new AiXiuDbContext();
        /// <summary>
        /// 用户名登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public OperResult<TBUsers> Login(string UserName, string Password)
        {
            //非空验证
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            {
                return OperResult<TBUsers>.Failed("用户名和密码不能为空");
            }
            //通过用户名查询用户，不存在返回错误信息
            LoginDAL LoginDAL = new LoginDAL();
            TBLogins tb = LoginDAL.ExixtsName(UserName);
            if (tb == null)
            {
                return OperResult<TBUsers>.Failed("用户名错误");
            }
            //验证加密
            SHAEncryption sha = new SHAEncryption();
            string en = sha.SHA1Encrypt(Password);
            if (sha.SHA1Encrypt(Password)!=tb.Password)
            {
                return OperResult<TBUsers>.Failed("密码错误");
            }
            TBUsersBLL bll = new TBUsersBLL();

            TBUsers user = bll.dataget(tb.Id);
            return OperResult<TBUsers>.Succeed(user);
        }

      /// <summary>
      /// 手机号登录
      /// </summary>
      /// <param name="UserPhone"></param>
      /// <param name="Password"></param>
      /// <returns></returns>
        public OperResult<TBUsers> Login1(string UserPhone, string Password)
        {
            //非空验证
            if (string.IsNullOrEmpty(UserPhone) || string.IsNullOrEmpty(Password))
            {
                return OperResult<TBUsers>.Failed("用户名和密码不能为空");
            }
            //通过用户名查询用户，不存在返回错误信息
            LoginDAL LoginDAL = new LoginDAL();
            TBLogins tb = LoginDAL.ExixtsPhone1(UserPhone);
            if (tb == null)
            {
                return OperResult<TBUsers>.Failed("手机号错误");
            }
            //验证加密
            SHAEncryption sha = new SHAEncryption();
            string en = sha.SHA1Encrypt(Password);
            if (tb.Password != en)
            {
                return OperResult<TBUsers>.Failed("密码错误");
            }
            TBUsersBLL bll = new TBUsersBLL();

            TBUsers user = bll.dataget(tb.Id);
            return OperResult<TBUsers>.Succeed(user);
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <returns></returns>
        public OperResult Result(TBLogins login) {
            //参数非空验证
            if (string.IsNullOrEmpty(login.UserName))
            {
                return OperResult.Failed("用户名不能为空");
            }
            if (string.IsNullOrEmpty(login.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空");
            }
            if (string.IsNullOrEmpty(login.Password))
            {
                return OperResult.Failed("密码不能为空");
            }

            //判断一下用户是否已经注册
            LoginDAL LoginDAL = new LoginDAL();
            if (LoginDAL.ExistsUserName(login.UserName)) {
                return OperResult.Failed("用户名已经注册");
            }
            if (LoginDAL.ExixtsPhone(login.MobileNumber))
            {
                return OperResult.Failed("手机号已经注册");
            }
            //加密密码
            SHAEncryption tion = new SHAEncryption();
            login.Password = tion.SHA1Encrypt(login.Password);
            //注册
            try
            {
                LoginDAL.ReZC(login);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
                return OperResult.Failed("注册失败:失败信息"+ex.ToString());
            }
            return OperResult.Succeed();
        }
      
    }
}
